import API from '../../api/movies/api_movies';
import * as echarts from 'echarts'
import options from '../components/option.js'
export default {
    data() {
        return {
            carouselValue: 0,
            countDateSelected: 'Mon Jan 01 2022 00:00:00 GMT+0800 (中国标准时间)',
            moviesTypeCheckedArr: [],
            moviesAreaCheckedArr: [],
            moviesAreaSelectArr: ["爱尔兰"],
            moviesTypeSelectArr: [],
            countDateEchart: null,
            typeCountEchart: null,
            typeActorsEchart: null,
            areaCountEchart: null,
            // moviesAreaCheckedArr: [],
            carouselList: [
                {
                    "src": require("@/assets/movies/top_carousel_1.png")
                },
                {
                    "src": require("@/assets/movies/top_carousel_2.jpg")
                },
                {
                    "src": require("@/assets/movies/top_carousel_3.jpg")
                }
            ],
            scoreList: [],
            moviesTypeArr: [],
            moviesAreaArr: []
        };
    },
    created() {
    },
    methods: {
        toBigScreen() {
            let that = this;
            let routeData = that.$router.resolve({ name: 'bigScreen' });
            window.open(routeData.href, '_blank');
        },
        toMovieList() {
            let that = this;
            that.$router.push({ name: 'movieList' })
        },
        toDataOverview() {
            let that = this;
            that.$router.push({ name: 'dataOverview' })
        },
        moviesTypeCheckedChange() {
            let that = this;
            console.log(that.moviesTypeCheckedArr)
        },
        moviesAreaCheckedChange() {
            let that = this;
            console.log(that.moviesAreaCheckedArr)
        },
        searchScoreData() {
            let that = this;
            that.scoreList = [];
            that.scoreList  = [{
                "title": "最高分",
                "value": 9.8,
                "img": require("@/assets/movies/score-max-img.jpg")
            },
            {
                "title": "最低分",
                "value": 2.1,
                "img": require("@/assets/movies/score-min-img.jpg")
            },
            {
                "title": "中位数",
                "value": 5.6,
                "img": require("@/assets/movies/score-med-img.jpg")
            },
            {
                "title": "平均分",
                "value": 5.1,
                "img": require("@/assets/movies/score-avg-img.jpg")
            }
            ];
            for (let i = 1; i < 5; i++) {
                API.getMoviesScore(i).then(
                    function (r) {
                        if (r && r.code === 200) {
                            that.scoreList[i-1].value=r.content;
                        } else {
                            that.$Message.error(r.message);
                        }
                    },
                    function (err) {
                    
                        that.$Message.error(err.toString());
                    }
                );
            }
        },
        searchSelectData() {
            let that = this;
            that.moviesTypeArr = [];
            that.moviesAreaArr = [];
            API.getAllTypes({}).then(
                function (r) {
                    if (r && r.code === 200) {
                        that.moviesTypeArr = r.content;
                    } else {
                        that.$Message.error(r.message);
                    }
                },
                function (err) {
                    that.$Message.error(err.toString());
                }
            );
            API.getAllAreas({}).then(
                function (r) {
                    if (r && r.code === 200) {
                        that.moviesAreaArr = r.content;
                    } else {
                        that.$Message.error(r.message);
                    }
                },
                function (err) {
                    that.moviesTypeArr = [
                        "家庭",
                        "歌舞",
                        "西部",
                        "荒诞",
                        "同性",
                        "爱情",
                        "古装",
                        "戏曲",
                        "黑色电影",
                        "儿童",
                        "奇幻",
                        "动画",
                        "科幻",
                        "武侠",
                        "脱口秀",
                        "短片",
                        "历史",
                        "悬疑",
                        "运动",
                        "动作",
                        "情色",
                        "音乐",
                        "真人秀",
                        "犯罪",
                        "冒险",
                        "战争",
                        "纪录片",
                        "惊悚",
                        "剧情",
                        "传记",
                        "灾难",
                        "恐怖",
                        "喜剧"
                    ];
                    that.moviesAreaArr = [
                        "约旦",
                        "阿根廷",
                        "丹麦",
                        "韩国",
                        "中国香港",
                        "匈牙利",
                        "爱沙尼亚",
                        "西班牙",
                        "阿富汗",
                        "捷克",
                        "摩纳哥",
                        "美国",
                        "斯洛文尼亚",
                        "捷克斯洛伐克",
                        "波兰",
                        "西德",
                        "比利时",
                        "中国大陆",
                        "土耳其",
                        "墨西哥",
                        "芬兰",
                        "秘鲁",
                        "黎巴嫩",
                        "伊朗",
                        "博茨瓦纳",
                        "卢森堡",
                        "阿联酋",
                        "阿尔及利亚",
                        "瑞典",
                        "法国",
                        "印度尼西亚",
                        "摩洛哥",
                        "俄罗斯",
                        "塞尔维亚",
                        "巴哈马",
                        "白俄罗斯",
                        "塞内加尔",
                        "泰国",
                        "越南",
                        "澳大利亚",
                        "巴西",
                        "希腊",
                        "意大利",
                        "新加坡",
                        "马来西亚",
                        "荷兰",
                        "南斯拉夫",
                        "日本",
                        "瑞士",
                        "中国澳门",
                        "爱尔兰",
                        "塞浦路斯",
                        "葡萄牙",
                        "保加利亚",
                        "苏联",
                        "卡塔尔",
                        "古巴",
                        "马耳他",
                        "英国",
                        "挪威",
                        "南非",
                        "东德",
                        "委内瑞拉",
                        "新西兰",
                        "奥地利",
                        "罗马尼亚",
                        "德国",
                        "中国",
                        "印度",
                        "巴基斯坦",
                        "加拿大",
                        "中国台湾"
                    ];
                    that.$Message.error(err.toString());
                }
            );
        },
        countDateSelectedChange() {
            let that = this;
            let date = new Date(that.countDateSelected);
            API.getCountByDate(date.getFullYear()).then(
                function (r) {
                    if (r && r.code === 200) {
                        let dates = Object.keys(r.content);
                        dates.sort();
                        let seriesData = [];
                        dates.forEach(element => {
                            seriesData.push(r.content[element]);
                        });
                        let countDateOption = options.getCountDateOption(dates, seriesData);
                        that.countDateEchart.setOption(countDateOption, true);
                    } else {
                        that.$Message.error(r.message);
                    }
                },
                function (err) {
                    let dates = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12'];
                    let seriesData = [20, 32, 91, 34, 12, 33, 20, 34, 12, 20, 32, 34];
                    let countDateOption = options.getCountDateOption(dates, seriesData);
                    that.countDateEchart.setOption(countDateOption, true);
                    that.$Message.error(err.toString());
                }
            );
        },
        moviesAreaSelectChange() {
            let that = this;
            API.getCountByArea(that.moviesAreaSelectArr).then(
                function (r) {
                    if (r && r.code === 200) {
                        let dates = Object.keys(r.content);
                        let seriesData = [];
                        dates.forEach(element => {
                            seriesData.push({
                                name: element,
                                value: r.content[element]
                            });
                        });
                        let typeCountOption = options.getTypeCountOption(seriesData, that.typeCountEchart);
                        that.typeCountEchart.setOption(typeCountOption, true);
                    } else {
                        that.$Message.error(r.message);
                    }
                },
                function (err) {
                    let seriesData = [
                        {
                            name: '情色',
                            value: '3'
                        }, {
                            name: '喜剧',
                            value: '340'
                        }, {
                            name: '科幻',
                            value: '149'
                        }, {
                            name: '运动',
                            value: '13'
                        }, {
                            name: '恐怖',
                            value: '62'
                        }, {
                            name: '儿童',
                            value: '5'
                        }, {
                            name: '犯罪',
                            value: '151'
                        }, {
                            name: '惊悚',
                            value: '182'
                        }, {
                            name: '冒险',
                            value: '216'
                        }, {
                            name: '恐怖',
                            value: '62'
                        }
                    ];
                    let typeCountOption = options.getTypeCountOption(seriesData, that.typeCountEchart);
                    that.typeCountEchart.setOption(typeCountOption, true);
                    that.$Message.error(err.toString());
                }
            );
        },
        moviesTypeSelectChange() {
            let that = this;
            API.getCountByType(that.moviesTypeSelectArr).then(
                function (r) {
                    if (r && r.code === 200) {
                        let dates = Object.keys(r.content);
                        let seriesData = [];
                        dates.reverse()
                        dates.forEach(element => {
                            seriesData.push(r.content[element]);
                        });
                        let areaCountOption = options.getAreaCountOption(dates, seriesData)
                        that.areaCountEchart.setOption(areaCountOption, true);
                    } else {
                        that.$Message.error(r.message);
                    }
                },
                function (err) {
                    let xData2 = ['泰国', '瑞士', '波兰', '奥地利', '泰国', '瑞士', '波兰', '奥地利', '泰国', '瑞士', '波兰', '奥地利', '泰国', '瑞士', '波兰', '奥地利', '泰国', '瑞士', '波兰', '奥地利', '泰国', '瑞士', '波兰', '奥地利', '泰国', '瑞士', '波兰', '奥地利', '泰国', '瑞士', '波兰', '奥地利', '泰国', '瑞士', '波兰', '奥地利', '泰国', '瑞士', '波兰', '奥地利', '泰国', '瑞士', '波兰', '奥地利', '泰国', '瑞士', '波兰', '奥地利', '泰国', '瑞士', '波兰', '奥地利', '泰国', '瑞士', '波兰', '奥地利', '泰国', '瑞士', '波兰', '奥地利', '泰国', '瑞士', '波兰', '奥地利', '泰国', '瑞士', '波兰', '奥地利', '泰国', '瑞士', '波兰', '奥地利'];
                    let seriesData3 = [40, 80, 20, 16, 40, 80, 20, 16, 40, 80, 20, 16, 40, 80, 20, 16, 40, 80, 20, 16, 40, 80, 20, 16, 40, 80, 20, 16, 40, 80, 20, 16, 40, 80, 20, 16, 40, 80, 20, 16, 40, 80, 20, 16, 40, 80, 20, 16, 40, 80, 20, 16, 40, 80, 20, 16, 40, 80, 20, 16, 40, 80, 20, 16, 40, 80, 20, 16, 40, 80, 20, 16];
                    let areaCountOption = options.getAreaCountOption(xData2, seriesData3)
                    that.areaCountEchart.setOption(areaCountOption, true);
                    that.$Message.error(err.toString());
                }
            );
        },
        initEchart() {
            let that = this;
            that.countDateEchart = echarts.init(document.getElementById('countDate_echart'));
            that.countDateSelectedChange();
            that.typeCountEchart = echarts.init(document.getElementById('type_count_echart'));
            that.moviesAreaSelectChange();
            that.areaCountEchart = echarts.init(document.getElementById('area_count_echart'));
            that.moviesTypeSelectChange();
            that.typeActorsEchart = echarts.init(document.getElementById('top_actors_echart'));
            API.getTopActors({}).then(
                function (r) {
                    if (r && r.code === 200) {
                        let Xdate1 = Object.keys(r.content);
                        Xdate1 = Xdate1.reverse();
                        let seriesData1 = [];
                        Xdate1.forEach(element => {
                            seriesData1.push(r.content[element]);
                        });
                        let typeActorsOption = options.getTypeActorsOption(Xdate1, seriesData1);
                        that.typeActorsEchart.setOption(typeActorsOption, true);
                    } else {
                        that.$Message.error(r.message);
                    }
                },
                function (err) {
                    let yData = ['成龙', '刘德华', '周星驰', '周润发', '吴孟达', '吴君如'];
                    let seriesData2 = [37, 39, 40, 47, 57, 61];
                    let typeActorsOption = options.getTypeActorsOption(yData, seriesData2);
                    that.typeActorsEchart.setOption(typeActorsOption, true);
                    that.$Message.error(err.toString());
                }
            );
        },
        initSearch() {
            let that = this;
            that.searchScoreData();
            that.searchSelectData();
            that.initEchart();
        },
    },
    mounted() {
        this.initSearch();
    }
};
